package com.changgou.order.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.pojo.Sku;
import com.changgou.order.feign.CartFeign;
import com.changgou.order.pojo.Order;
import com.changgou.order.pojo.OrderItem;
import com.changgou.user.feign.UserFeign;
import com.changgou.user.pojo.Address;
import com.changgou.util.SMSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/worder")
public class OrderController {

	@Autowired
	private UserFeign userFeign;
	@Autowired
	private CartFeign cartFeign;
	@RequestMapping("/ready/order")
	public String readyOrder(Model model){
		//收件人地址信息
		List<Address> addressList = userFeign.list().getData();
		for (Address address : addressList) {
			if(address.getIsDefault().equals("1")){
				model.addAttribute("deAddr",address);
				break;
			}
		}
		model.addAttribute("address",addressList);
		//购物车信息
		Map map = cartFeign.list();
		List<OrderItem> orderItemList = (List<OrderItem>) map.get("orderItemList");
		Integer totalPrice = (Integer) map.get("totalPrice");
		Integer totalNum = (Integer) map.get("totalNum");
		model.addAttribute("carts",orderItemList);
		model.addAttribute("totalPrice",totalPrice);
		model.addAttribute("totalNum",totalNum);
		return "order";
	}
	@PostMapping("/add")
	@ResponseBody
	public Result addOrder(@RequestBody Order order){
		Result result = cartFeign.addOrder(order);
		return result;
	}
	@GetMapping("/toPayPage")
	public String toPayPage(String orderId, Model model){
		Result<Order> orderResult = cartFeign.findById(orderId);
		Order order = orderResult.getData();
		model.addAttribute("orderId",orderId);
		model.addAttribute("payMoney",order.getPayMoney());
		return "pay";
	}
	@GetMapping("/mycollection")
	public String listCollection(Model model){
		List<Sku> skuList = (List<Sku>) cartFeign.listCollection().getData();
		model.addAttribute("skuList",skuList);
		return "center-collect";
	}
	@GetMapping("/myfootmark")
	public String listFootMark(Model model){
		List<Sku> skuList = (List<Sku>) cartFeign.listFootMark().getData();
		model.addAttribute("skus",skuList);
		return "center-footmark";
	}
	//待付款
	@GetMapping("/toOrderPay")
	public String toOrderPay(String orderId,Model model){
		Result result = cartFeign.findAllItemByOrder();
		List<Map> orderList = (List<Map>) result.getData();
		List<Map> resultMap = new  ArrayList<>();
		for (Map map : orderList) {
			if("0".equals(map.get("payStatus"))){
				resultMap.add(map);
			}
		}
		model.addAttribute("resultMap",resultMap);
		return "center-order-pay";
	}
	//待发货
	@GetMapping("/toOrderSend")
	public String toOrderSend(Model model){
		Result result = cartFeign.findAllItemByOrder();
		List<Map> orderList = (List<Map>) result.getData();
		List<Map> resultMap = new  ArrayList<>();
		for (Map map : orderList) {
			if("1".equals(map.get("payStatus"))&&"0".equals(map.get("consignStatus"))){
				resultMap.add(map);
			}
		}
		model.addAttribute("resultMap",resultMap);
		return "center-order-send";
	}
	//待收货
	@GetMapping("/toOrderReceive")
	public String toOrderReceive(Model model){
		Result result = cartFeign.findAllItemByOrder();
		List<Map> orderList = (List<Map>) result.getData();
		List<Map> resultMap = new  ArrayList<>();
		for (Map map : orderList) {
			if("1".equals(map.get("payStatus"))&&"1".equals(map.get("consignStatus"))){
				resultMap.add(map);
			}
		}
		model.addAttribute("resultMap",resultMap);
		return "center-order-receive";
	}
	//待评价
	@GetMapping("/toOrderEvaluate")
	public String toOrderEvaluate(Model model){
		Result result = cartFeign.findAllItemByOrder();
		List<Map> orderList = (List<Map>) result.getData();
		List<Map> resultMap = new  ArrayList<>();
		for (Map map : orderList) {
			if("1".equals(map.get("payStatus"))&&"2".equals(map.get("consignStatus"))){
				resultMap.add(map);
			}
		}
		model.addAttribute("resultMap",resultMap);
		return "center-order-evaluate";
	}
	@GetMapping("/toOrderPage")
	public String toOrderPage(Model model){
		Result result = cartFeign.findAllItemByOrder();
		List<Map> orderList = (List<Map>) result.getData();
		model.addAttribute("orderList",orderList);
		return "center-index";
	}
	@RequestMapping("/take")
	public String take(String orderId,String operator,Model model){
		cartFeign.take(orderId, operator);
		Result result = cartFeign.findAllItemByOrder();
		List<Map> orderList = (List<Map>) result.getData();
		List<Map> resultMap = new  ArrayList<>();
		for (Map map : orderList) {
			if("1".equals(map.get("payStatus"))&&"1".equals(map.get("consignStatus"))){
				resultMap.add(map);
			}
		}
		model.addAttribute("resultMap",resultMap);
		return "center-order-receive";
	}
	@RequestMapping("/closeOrder")
	public String closeOrder(String orderId,Model model){
		cartFeign.closeOrder1(orderId);
		Result result = cartFeign.findAllItemByOrder();
		List<Map> orderList = (List<Map>) result.getData();
		List<Map> resultMap = new ArrayList<>();
		for (Map map : orderList) {
			if("0".equals(map.get("payStatus"))){
				resultMap.add(map);
			}
		}
		model.addAttribute("resultMap",resultMap);
		return "center-order-pay";
	}

	@RequestMapping("/send")
	@ResponseBody
	public Result sendMessage(String orderId){
		try{
			SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,"17624072131",orderId);
			return new Result(true, StatusCode.OK,"提醒发货成功");
		}catch (Exception e){
			e.printStackTrace();
			return new Result(false, StatusCode.ERROR,"操作失败");
		}
	}
}
